package com.tuyan.aixiang.util;

import android.app.Activity;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.tuyan.aixiang.MainApplication;

/**
 * operate shared preference
 */
public class StorageUtil {
    private static final String TAG = "Storage";

    private static final String SHARED_PREFERENCE_NAME = "baoyou_shared_pref";

    private static SharedPreferences.Editor getEditor() {
        SharedPreferences preferences = MainApplication.getAppContext()
                .getSharedPreferences(SHARED_PREFERENCE_NAME,
                        Activity.MODE_PRIVATE);
        return preferences.edit();
    }

    public static boolean setBoolean(String key, Boolean val) {
        SharedPreferences.Editor editor = getEditor();
        try {
            editor.putBoolean(encryptKey(key), val);
            return editor.commit();
        } catch (Exception e) {
            return false;
        }
    }


    public static boolean getBoolean(String key, boolean dft) {
        SharedPreferences preferences = MainApplication.getAppContext()
                .getSharedPreferences(SHARED_PREFERENCE_NAME,
                        Activity.MODE_PRIVATE);
        try {
            return preferences.getBoolean(encryptKey(key), dft);
        } catch (Exception e) {
            return false;
        }
    }

    public static boolean setInt(String key, Integer val) {
        SharedPreferences.Editor editor = getEditor();
        try {
            editor.putInt(encryptKey(key), val);
            return editor.commit();
        } catch (Exception e) {
            return false;
        }
    }


    public static int getInt(String key, int dft) {
        SharedPreferences preferences = MainApplication.getAppContext()
                .getSharedPreferences(SHARED_PREFERENCE_NAME,
                        Activity.MODE_PRIVATE);
        try {
            return preferences.getInt(encryptKey(key), dft);
        } catch (Exception e) {
            return dft;
        }
    }

    public static boolean setString(String key, String val) {
        SharedPreferences.Editor editor = getEditor();
//        editor.putString(key, val);
        try {
            setString(editor, key, val);
            return editor.commit();
        } catch (Exception e) {
            return false;
        }
    }

    public static String getString(String key, String dft) {
        SharedPreferences preferences = MainApplication.getAppContext()
                .getSharedPreferences(SHARED_PREFERENCE_NAME,
                        Activity.MODE_PRIVATE);
        try {
            return getString(preferences, key, dft);
        } catch (Exception e) {
            Log.e(TAG, e);
            return dft;
        }
//        return preferences.getString(key, dft);
    }

    public static <T> boolean setObject(String key, T obj) {
        SharedPreferences.Editor editor = getEditor();
        try {
            String val = new Gson().toJson(obj);
//            editor.putString(key, val);
            setString(key, val);
            return editor.commit();

        } catch (JsonParseException e) {
            Log.e(TAG, e);
            return false;
        }
    }

    public static <T> T getObject(String key, Class<T> clazz) {

        try {
            SharedPreferences preferences = MainApplication.getAppContext()
                    .getSharedPreferences(SHARED_PREFERENCE_NAME,
                            Activity.MODE_PRIVATE);
//            String json = preferences.getString(key, null);
            String json = getString(preferences, key, null);
            if (null == json) return null;
            return new Gson().fromJson(json, clazz);
        } catch (Exception e) {
            Log.e(TAG, e);
            return null;
        }
    }

    public static boolean remove(String key) {
        SharedPreferences.Editor editor = getEditor();
        try {
            editor.remove(AESUtil.encrypt(key, null));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return editor.commit();
    }

    public static boolean clear() {
        Log.w(TAG, "clear sharedPreference");
        SharedPreferences.Editor editor = getEditor();
        editor.clear();
        return editor.commit();
    }

    private static void setString(SharedPreferences.Editor editor, String key, String val) throws Exception {
        editor.putString(AESUtil.encrypt(key, null), AESUtil.encrypt(val, null));
    }

    private static String getString(SharedPreferences pref, String key, String dft) throws Exception {
        String originStr = pref.getString(AESUtil.encrypt(key, null), dft);
        if (null == originStr) return null;
        return AESUtil.decrypt(originStr, null);
    }

    private static String encryptKey(String key) throws Exception {
        return AESUtil.encrypt(key, null);
    }

}
